﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using MTechApplicationServices.Contracts;
using MTechApplicationServices.Data;

namespace MTechApplicationServices.Activities
{

    public sealed class SaveMTechApplication : CodeActivity<SubmitMTechApplicationResponse>
    {
        
        public InArgument<SubmitMTechApplicationRequest> AppRequest { get; set; }
        
        protected override SubmitMTechApplicationResponse Execute(CodeActivityContext context)
        {
            using (MTechApplicationDataEntities ctx = new MTechApplicationDataEntities())
            {
                SubmitMTechApplicationRequest request = AppRequest.Get(context);
                Applicant app = ctx.Applicants.CreateObject();
                app.ApplicantName = request.Info.Name;
                app.Email = request.Info.Email;
                app.IDNo = request.Info.IDNo;
                app.HighestDegree = request.Info.HighestDegree;
                app.Program = request.Info.Program;
                app.Experience = request.Info.Experience;
                app.MediumOfInstruction = request.Info.MediumOfInstruction;
                app.EnglishTestType = request.Info.EnglishTestType;
                app.EnglishTestScore = request.Info.EnglishTestScore;
                app.RequestID = request.RequestID;

                ctx.Applicants.AddObject(app);
                ctx.SaveChanges();
                ctx.Connection.Close();

                return new SubmitMTechApplicationResponse()
                {
                    ApplicationID = app.ApplicationID,
                    ApplicantName = request.Info.Name,
                    ResponseText = string.Format(ServiceResources.MTechApplicationProcessing, request.Info.Name, app.ApplicationID)
                };
            }
        }
    }
}
